//
//  AVPlayerLayerViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/25.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "AVPlayerLayerViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>

@interface AVPlayerLayerViewController ()

@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) AVPlayer *player; // 播放器

@property (nonatomic, strong) AVPlayerLayer *avPlayerLayer; // 图形层

@end

@implementation AVPlayerLayerViewController

/**
 *  最后一个图层类型是AVPlayerLayer。尽管它不是Core Animation框架的一部分（AV前缀看上去像），AVPlayerLayer是有别的框架（AVFoundation）提供的，它和Core Animation紧密地结合在一起，提供了一个CALayer子类来显示自定义的内容类型。
 
 AVPlayerLayer是用来在iOS上播放视频的。他是高级接口例如MPMoivePlayer的底层实现，提供了显示视频的底层控制。AVPlayerLayer的使用相当简单：你可以用+playerLayerWithPlayer:方法创建一个已经绑定了视频播放器的图层，或者你可以先创建一个图层，然后用player属性绑定一个AVPlayer实例。
 当然，因为AVPlayerLayer是CALayer的子类，它继承了父类的所有特性。我们并不会受限于要在一个矩形中播放视频；我们可以添加CALayer的3D，圆角，有色边框，蒙板，阴影等效果
 */

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.player pause];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.backgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    self.backgroundView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_backgroundView];
 
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL: [[NSBundle mainBundle] URLForResource:@"中英字幕 Taylor.Swift--Sparks Fly  现场版 标清" withExtension:@"mp4"]];
    self.player = [AVPlayer playerWithPlayerItem:item];
    
    self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    self.avPlayerLayer.frame = CGRectMake(10, 50, self.view.frame.size.width - 20, 250);
    self.avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspect; // 图层的视频对齐方式
    [self.backgroundView.layer addSublayer:self.avPlayerLayer];
    
    // 我们为avplayer设置一个阴影，这样能够清楚的看到范围
    self.avPlayerLayer.shadowColor = [UIColor redColor].CGColor;
    self.avPlayerLayer.shadowOffset = CGSizeMake(0, 0);
    self.avPlayerLayer.shadowOpacity = 1;

    
    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    playButton.frame = CGRectMake(50, 400, 150, 30);
    [playButton setTitle:@"播放" forState:UIControlStateNormal];
    [playButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [playButton addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroundView addSubview:playButton];
    
    UIButton *changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeButton.frame = CGRectMake(150, 400, 150, 30);
    [changeButton setTitle:@"改变状态" forState:UIControlStateNormal];
    [changeButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [changeButton addTarget:self action:@selector(change:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroundView addSubview:changeButton];
    
    UIButton *restoreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    restoreButton.frame = CGRectMake(50, 450, 150, 30);
    [restoreButton setTitle:@"回复原状" forState:UIControlStateNormal];
    [restoreButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [restoreButton addTarget:self action:@selector(restore:) forControlEvents:UIControlEventTouchUpInside];
    [self.backgroundView addSubview:restoreButton];
}

- (void)play:(UIButton *)button
{
    [self.player play];
}

- (void)change:(UIButton *)button
{
    CATransform3D transform3D = CATransform3DIdentity;
    transform3D.m34 = - 1.0 / 500.0;
    transform3D = CATransform3DMakeRotation(M_PI_4, 1, 1, 0);
    self.avPlayerLayer.transform = transform3D;
}

- (void)restore:(UIButton *)button
{
    self.avPlayerLayer.transform = CATransform3DIdentity;
}





@end
